package leeon.bbs;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class IdsHelper {
	
	private static Log logger = LogFactory.getLog(IdsHelper.class);

	public static final String BASE_DIR = "D:\\project\\eurospace\\forpeddy\\bisc\\";
	private File dir = new File(BASE_DIR);
	
	public IdsHelper() {
	}
	
	public void parse() throws IOException {
		String[] files = dir.list(new FilenameFilter() {
			public boolean accept(File dir, String name) {
				if (name.startsWith("ids") && name.endsWith(".txt"))
					return true;
				else
					return false;
			}
		});
		logger.info("set define off;");
		for (String f : files) {
			parseOneFile(new File(BASE_DIR + f));
			logger.info("commit;");
		}
		logger.info("set define on;");
		
	}
	
	public void parseOneFile(File file) throws IOException {
		if (file.exists()) {
			BufferedReader reader = new BufferedReader(new FileReader(file));
			String line = null;
			while ((line = reader.readLine()) != null) {
				line = line.trim();
				if (!line.equals("")) {
					parseOneLine(line, file.getName().substring(3, 17));
				}
			}
			reader.close();
		}
	}
	
	public void parseOneLine(String line, String date) {
		if (line == null || line.length() == 0) return;
		if (line.startsWith("#")) return;
		
		String[] ids = line.split("[\\W]+");
		for (String id : ids) {
			System.out.println(id + ", date:[" + date + "]");
			logger.info("insert into bbsids_temp (id, scan_date) values ('" +
					id +
					"', to_date('" +
					date +
					"', 'yyyymmddhh24miss'));");
		}
	}
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		IdsHelper user = new IdsHelper();
		user.parse();
	}

}
